package apidayfile;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Scanner;


public class FileDemo02 {
    public static void main(String[] args) throws IOException {
        FileDemo02 f =new FileDemo02();
        f.createNewFile();//创建100个文件
        f.deleteNewFile();
        f.createInputFile();
        f.deleteInputFile();
        f.createInputNewFile();
        f.checkAnonymousS();
        f.checkLambdaS();

    }



    //创建100个文件方法
    void createNewFile() throws IOException {
        for (int i = 1; i <= 100 ; i++) {
            File file = new File("test"+i+".txt");
            if(file.isFile()){
                System.out.println("该文件已近创建");
            }else {
                file.createNewFile();
            }
        }
    }


    //删除100个文件方法
    void deleteNewFile(){
        for (int i = 1; i <=100 ; i++) {
            File file = new File("test"+i+".txt");
            if (file.exists()){
                file.delete();
            }
        }
    }


    //用户输入创建目录
    void createInputFile(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要创建的目录名称");
        String input = scan.next();
        File file = new File(input);
        file.mkdirs();
    }


    //删除用户输入目录
    void deleteInputFile(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要删除的目录名称");
        String input = scan.next();
        File file = new File(input);
        file.delete();
    }


    //用户输入创建一个新的目录
    void createInputNewFile() throws IOException{
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要创建的文件名称");

        while (true){
            String input = scan.next();
            File file = new File(input);
            if (file.exists()){
                System.out.println("文件名重复，请重新输入");
            }else{
                file.createNewFile();
                break;
            }
        }
    }


    //检索包含"s"的子项----匿名内部类
    void checkAnonymousS(){
        File file = new File(".");
        if(file.exists()){
            File [] files = file.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().contains("s");
//                    return pathname.getName().indexOf("s")!=-1;

                }
            });
            for (File f:files
                 ) {
                System.out.println("匿名内部类方法："+f.getName());
            }
        }
    }


    //检索包含"s"的子项----Lambda方法
    void checkLambdaS(){
        File file = new File(".");
        File [] files = file.listFiles(pathname -> pathname.getName().contains("s"));
        for (File f: files
             ) {
            System.out.println("Lambda方法："+f.getName());
        }
    }



}
